import wxValidate from 'wechat-validate'
Page({
  behaviors: [wxValidate],
  data: {
    currentDate: Date.now(),
    houseLayerVisible: false,
    repairLayerVisible: false,
    dateLayerVisible: false,
    houseList: [],
    repairItem: [],
    attachment: [],
    houseId: '',
    houseInfo: '',
    repairItemId: '',
    repairItemName: '',
    mobile: '',
    description: '',
    appointment: ''
  },
  rules: {
    houseId: [
      { required: true, message: '请选择保修房屋！' }
    ],
    repairItemId: [
      { required: true, message: '请选择维修的项目！' }
    ],
    mobile: [
      { required: true, message: '请填写手机号码！' },
      { pattern: /^1[3-8]\d{9}$/, message: '请填写正确的手机号码！' }
    ],
    appointment: [
      { required: true, message: '请选择预约日期！' }
    ],
    description: [
      { required: true, message: '请填写问题描述！' }
    ]
  },
  onLoad ({id}) {
    this.getHouseList()
    this.getRepairItem()
    if (id) this.getRepairDetail(id)
  },

  async getRepairDetail (id) {
    const {code, data: repairDetail} = await wx.http.get('/repair/' + id)
    if (code !== 10000) return wx.utils.toast()
    this.setData({
      ...repairDetail
    })
  },

  async getHouseList () {
    const {code, data: houseList} = await wx.http.get('/house')
    if (code !== 10000) return wx.utils.toast()
    this.setData({
      houseList
    })
  },

  selectHouseInfo(ev) {
    this.setData({
      houseId: ev.detail.id,
      houseInfo: ev.detail.name
    })
  },

  async getRepairItem () {
    const {code, data: repairItem} = await wx.http.get('/repairItem')
    if (code !== 10000) return wx.utils.toast()
    this.setData({
      repairItem
    })
  },

  selectRepairItemInfo (ev) {
    this.setData({
      repairItemId: ev.detail.id,
      repairItemName: ev.detail.name
    })
  },
  openHouseLayer() {
    this.setData({ houseLayerVisible: true })
  },
  closeHouseLayer() {
    this.setData({ houseLayerVisible: false })
  },
  openRepairLayer() {
    this.setData({ repairLayerVisible: true })
  },
  closeRepairLayer() {
    this.setData({
      repairLayerVisible: false,
    })
  },

  selectDateInfo (ev) {
    this.setData({ appointment: wx.utils.dataFormat(ev.detail), dateLayerVisible: false })
  },

  openDateLayer() {
    this.setData({ dateLayerVisible: true })
  },
  closeDateLayer() {
    this.setData({ dateLayerVisible: false })
  },
  uploadPicture (ev) {
    const {file} = ev.detail
    wx.uploadFile({
      url: wx.http.baseURL + '/upload',
      filePath: file.url,
      name: 'file',
      header: {
        Authorization: 'Bearer ' + getApp().token
      },
      success: (result) => {
        const data= JSON.parse(result.data)
        if (data.code !== 10000) return wx.utils.toast('文件上传失败！')
        const {attachment} = this.data
        attachment.push(data.data)
        this.setData({
          attachment
        })
      }
    })
  },
  async submitForm() {
    if (!this.validate()) return
    const {id, houseId, repairItemId, mobile, appointment, description, attachment} = this.data
    const {code} = await wx.http.post('/repair', {
      id, houseId, repairItemId, mobile, appointment, description, attachment
    })
    if (code !== 10000) return wx.utils.toast('在线报修失败！')
    wx.redirectTo({
      url: '/repair_pkg/pages/list/index'
    })
  },
})
